package com.donghaiair.framework.common.util;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

/**
 * @author Cheung 推送工具类
 */
@Component
public class PushUtils {

	@Autowired
	private PushConfigUtil config;

	/**
	 * 个推-移动端消息推送工具类
	 * 
	 * @param title
	 *            消息推送的标题
	 * @param text
	 *            消息推送的内容
	 * @param url
	 *            消息推送消息体的跳转路径
	 * @param target
	 *            推送目标集合
	 * @return IPushResult 消息推送的处理结果
	 */
	@SuppressWarnings("deprecation")
	public IPushResult pushList(String title, String text, String url, List<Target> targets, String content) {
		IGtPush push = new IGtPush(config.getAppUrl(), config.getAppKey(), config.getMasterSecret());

		TransmissionTemplate template = getTemplate(title, text, url, content);
		ListMessage message = new ListMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		message.setPushNetWorkType(0);

		/*
		 * List<Target> targets = new ArrayList<Target>(); Target target = new
		 * Target(); target.setAppId(config.getAppId());
		 * target.setClientId(empId);
		 * 
		 * targets.add(target);
		 */

		/*
		 * Target target3 = new Target(); target3.setAppId(config.getAppId());
		 * target3.setClientId("11153318b12d9531ebeb988788ad5d33"); Target
		 * target4 = new Target(); target4.setAppId(config.getAppId());
		 * target4.setClientId("9d6de605316ded5c36a060f73415989");
		 * target.add(target2); target.add(target3); target.add(target4);
		 */
		// target.setAlias(Alias);
		IPushResult ret = null;
		try {
			String taskId = push.getContentId(message);
			ret = push.pushMessageToList(taskId, targets);
		} catch (RequestException e) {
			e.printStackTrace();
		}
		if (ret != null) {
			System.out.println(ret.getResponse().toString());
		} else {
			System.out.println("服务器响应异常");
		}
		// System.out.println(ret.getResponse().toString());
		// 返回结果示例{result=ok, contentId=OSA-0909_r16UiSd8mk5dJpnT7p9MI7}
		return ret;
	}

	public IPushResult pushSingle(String title, String text, String url, String clientId, String content) {
		IGtPush push = new IGtPush(config.getAppUrl(), config.getAppKey(), config.getMasterSecret());

		TransmissionTemplate template = getTemplate(title, text, url, content);
		SingleMessage message = new SingleMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		message.setPushNetWorkType(0);
		Target target = new Target();
		target.setAppId(config.getAppId());
		target.setClientId(clientId);
		IPushResult ret = null;
		try {
			ret = push.pushMessageToSingle(message, target);
		} catch (RequestException e) {
			e.printStackTrace();
		}
		if (ret != null) {
			System.out.println(ret.getResponse().toString());
		} else {
			System.out.println("服务器响应异常");
		}
		// System.out.println(ret.getResponse().toString());
		// 返回结果示例{result=ok, contentId=OSA-0909_r16UiSd8mk5dJpnT7p9MI7}
		return ret;
	}

	// 透传信息
	public TransmissionTemplate getTemplate(String title, String text, String url, String content) {
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(config.getAppId());
		template.setAppkey(config.getAppKey());
		template.setTransmissionContent(content);
		template.setTransmissionType(2);
		APNPayload payload = new APNPayload();
		// 在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
		payload.setAutoBadge("+1");
		payload.setContentAvailable(1);
		payload.setSound("default");
		payload.setCategory(content);

		// 简单模式APNPayload.SimpleMsg
		// payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));

		// 字典模式使用APNPayload.DictionaryAlertMsg
		payload.setAlertMsg(getDictionaryAlertMsg(title, text));
		template.setAPNInfo(payload);
		return template;
	}

	private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String text) {
		APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
		alertMsg.setBody(text);
		// iOS8.2以上版本支持
		alertMsg.setTitle(title);
		return alertMsg;
	}
}
